from django.db import models


class InterviewRecord(models.Model):
    EVALUATION_LEVEL_CHOICES = (  # 评估等级选项‌:ml-citation{ref="2" data="citationList"}
        (1, 'A'),
        (2, 'B'),
        (3, 'C'),
        (4, 'D'),
        (5, 'E'),
    )

    user_id = models.IntegerField(
        verbose_name='用户ID',
        help_text='关联用户表的id,未制定约束,手动维护'
    )
    company_id = models.IntegerField(
        verbose_name='企业ID',
        help_text='关联企业表的id,未制定约束,手动维护'
    )

    total_score = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='总得分',
        help_text='0-100分的整数评分'
    )
    appearance_score = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='形象得分',
        help_text='0-20分的形象评分'
    )
    evaluation_level = models.PositiveSmallIntegerField(
        choices=EVALUATION_LEVEL_CHOICES,
        default=5,
        verbose_name='评估等级',
        help_text='A-E五级评估体系'
    )
    work_experience = models.TextField(  # 改用TextField存储长文本‌:ml-citation{ref="4" data="citationList"}
        null=True,
        blank=True,
        verbose_name='工作经验',
        help_text='详细工作经历描述'
    )
    interview_scope = models.CharField(
        max_length=255,
        null=True,
        blank=True,
        verbose_name='面试范围',
        help_text='面试的知识点范围'
    )

    audio_path = models.TextField(
        '音频文件',
        max_length=500,
        blank=True,
        null=True,
        help_text="招聘文件"
    )

    video_path = models.TextField(
        '视频文件',
        max_length=500,
        blank=True,
        null=True,
        help_text="招聘文件"
    )

    interview_process_file_path = models.TextField(
        '面试过程文档',
        max_length=500,
        blank=True,
        null=True,
        help_text="招聘文件"
    )

    evaluation_file_path = models.TextField(
        '评估报告文件',
        max_length=500,
        blank=True,
        null=True,
        help_text="评估报告文件"
    )


    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间',
        help_text='记录创建时间'
    )
    remarks = models.TextField(  # 改用Text存储长备注‌:ml-citation{ref="4" data="citationList"}
        null=True,
        blank=True,
        verbose_name='备注',
        help_text='附加说明信息'
    )
    type = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='面试状态',
        help_text='0-已预约，1-已面试'
    )
    class Meta:
        db_table = 'interview_record'  # 指定数据库表名‌:ml-citation{ref="3" data="citationList"}
        verbose_name = '面试记录'
        verbose_name_plural = '面试记录'
        indexes = [
            models.Index(fields=['created_at']),  # 常用查询索引‌:ml-citation{ref="6" data="citationList"}
        ]

    def __str__(self):
        return "%s的%s面试记录"%(self.user_id,self.company_id)

    @classmethod
    def get_companies_by_user0(cls, user_id):
        """
        根据用户ID获取所有关联的企业ID列表
        参数: user_id - 用户ID (int)
        返回: 去重的企业ID列表 (list[int])
        """
        return list(
            cls.objects
                .filter(user_id=user_id, type=0)
                .values_list('company_id', flat=True)
                .order_by('company_id')
        )

    @classmethod
    def get_student_interviews(cls, user_id):
        """
        获取学生面试记录及关联企业信息
        :param user_id: 学生ID
        :return: 结构化数据列表
        """
        # 获取学生所有面试记录
        interviews = cls.objects.filter(user_id=user_id,type=0).order_by('-created_at')

        # 提取所有关联的企业ID
        records = [(record.id,record.company_id,record.user_id) for record in interviews]
        data = []
        for r in records:
            c = Company.objects.get(id=r[1])
            data.append({"id":r[0],"user_id":r[2],"company_id":r[1],"name":c.name,"job_title":c.job_title,"location":c.location,"date_range":c.date_range})
        return data



class Company(models.Model):
    # 基础信息
    name = models.CharField('企业名称', max_length=200)
    job_title = models.CharField('岗位名称', max_length=100)

    location = models.CharField(
        '工作城市',  # [用户需求]
        max_length=100,  # ‌:ml-citation{ref="1,2" data="citationList"}
        help_text="如：北京市朝阳区、上海市浦东新区"
    )

    # 薪资范围（单位：元/月）
    salary_min = models.PositiveIntegerField('最低薪资')
    salary_max = models.PositiveIntegerField('最高薪资')

    # 时间信息
    start_date = models.DateField('招聘开始时间')
    end_date = models.DateField('招聘截止时间')

    # 面试配置
    INTERVIEW_FORMAT_CHOICES = [
        ('onsite', '现场面试'),
        ('online', '视频面试'),
        ('phone', '电话面试'),
    ]
    interview_format = models.CharField(
        '面试形式',
        max_length=20,
        choices=INTERVIEW_FORMAT_CHOICES,
        default='onsite'
    )

    # 详细要求
    requirements = models.TextField(
        '招聘要求',
        max_length=1000,
        help_text="请输入岗位具体要求（不超过1000字）"
    )

    # 其他信息
    remarks = models.TextField(
        '其他说明',
        max_length=500,
        blank=True,
        null=True,
        help_text="其他补充信息（可选）"
    )
    word_path = models.TextField(
        '招聘文件',
        max_length=500,
        blank=True,
        null=True,
        help_text="招聘文件"
    )

    # 元数据
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'company'  # 指定数据库表名
        verbose_name = '企业表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s-%s"%(self.name,self.job_title)

    @property
    def salary_range(self):
        """返回格式化的薪资范围"""
        return "%sk - %sk"%(self.salary_min,self.salary_max)

    @property
    def is_active(self):
        """检查招聘是否在有效期内"""
        from django.utils import timezone
        return self.start_date <= timezone.now().date() <= self.end_date

    @property
    def date_range(self):
        return "%s - %s"%(self.start_date,self.end_date)



